<?php
session_start();
require_once("Zend/Oauth/Consumer.php");
require_once("Zend/Service/Twitter.php");

$config=Array(
		'callbackUrl'=>'http://localhost/proiektua/favoresAjax/OAuth/newUser.php',
		'siteUrl'=>'https://twitter.com/oauth',
		'consumerKey'=>'Sx9ChGDVKI6SD6dSvnzw',
		'consumerSecret'=>'wwJPNwXhZMnFbrw1A2H7nlnqEI6TWwucRkbTYVo0eFM'
);


$consumer = new Zend_Oauth_Consumer($config);

if (!empty($_GET) && isset($_SESSION['TWITTER_REQUEST_TOKEN'])) {

	$token = $consumer->getAccessToken(
			$_GET,
			unserialize($_SESSION['TWITTER_REQUEST_TOKEN'])
	);

	$_SESSION['TWITTER_ACCESS_TOKEN'] = serialize($token);

	// Now that we have an Access Token, we can discard the Request Token

	$_SESSION['TWITTER_REQUEST_TOKEN'] = null;

	//Onaino ondo funtzionatzen du.


	//erabiltzailea sisteman dagoen ikusi behar da.

	require_once("../php/DBKonexioa.php");

	$con=new DBKonexioa();

	$con->konektatu();
	$sql="Select * FROM oAuthTwitter WHERE idoAuthTwitter='$token->user_id'";
	$res=mysql_query("Select * FROM oAuthTwitter WHERE idoAuthTwitter='$token->user_id'");
	$existe=mysql_num_rows($res);
	


	if($existe!="0"){
		//hemen home erabiltzaile orrira eramango da erabiltzailea
		//lehenik eta behin session[idUsr] esleitu.
		$usuario=mysql_fetch_assoc($res);
		
		$_SESSION['idUsr']=$usuario['fkUsuario'];
		
		header("Location: http://localhost/proiektua/favoresAjax/php/home.php");
	}else{
		//erbailtzailea sisteman behar diren eremuak bete beharko ditu. include zerbait

		include("OAuthForm.php");
	}


} else {
	// Mistaken request? Some malfeasant trying something?

	header("Location: http://localhost/proiektua/favoresAjax/index.php?errorea=1");
	exit ("Invalid callback request. Oops. Sorry.");


}



?>